package org.inasnet.frame;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Iterator;
import java.util.LinkedList;

import javax.swing.*;

import org.inasnet.FontManager;
import org.inasnet.Root;
import org.inasnet.user.ClientUser;
import org.inasnet.user.ContactGroup;
import org.inasnet.user.User;
import org.inasnet.user.User.Status;
import org.inasnet.util.*;

/**
 * Main frame of program, show contact list, have a menu.
 */
public class MainFrame extends JFrame// implements MouseListener
 implements ActionListener
{
	/**
	 * Menu overlapped class
	 */
	class Menu extends JMenu// implements ActionListener
	{
		Menu( String langIter, JMenuBar bar )
		{
			super( Root.lang( langIter ) );
			bar.add( this );
			
			//addActionListener( this );
		}
	}
	
	class UserConnButton extends JButton
	{
		UserConnButton( String title, User user )
		{
			super( title );
			mUser = user;
		}
		
		User mUser;

		public User getUser()
		{
			return mUser;
		}
	}
	
	/**
	 * Menu item overlapped class
	 */
	class MenuItem extends JMenuItem
	{
		MenuItem( String langIter, Menu menu )
		{
			super( Root.lang( langIter ) );
			menu.add( this );
			
			//addActionListener( menu );
		}
		
		MenuItem( String name )
		{
			super( name );
			//item.add( this );

			//addActionListener( ( Menu ) item );
		}
	}
	
	public MainFrame( ClientUser user )
	{
		mUser = user;
		
		setTitle( mTitle );
		
		setLocation( 600, 100 );
		setSize( mFrameWidth, mFrameHeight );
		//setUndecorated( true );
		
		setResizable( false );
		setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
		
		//setAlwaysOnTop( true );  
		
		setIconImage( Util.loadImage( "microphone_3.gif" ) );
		
		/*
		 * Menu
		 */
		//JMenuBar menuBar = new JMenuBar();

		//Menu menu = new Menu( "MAIN_Menu_Settings", menuBar );
		
		//JMenu m = new JMenu( "w", menu );
		
		//MenuItem contacts = new MenuItem( "MAIN_Menu_Settings_Contacts", menu );
		
		//MenuItem menuItem = new MenuItem( "MAIN_Menu_Settings_Profile", menu );
		
		//menuItem = new MenuItem( "MAIN_Menu_Settings_Connection", menu );
		
		
		
		/*
		 * End Menu
		 */
		
		/*
		 * Main content 
		 */
		
		// Calculate num rows of table 
	
		int needRows = 0;
		
		Iterator< org.inasnet.user.ContactGroup > groupsIter = Root.getUser().getContactList().getGroupsList().iterator();
		
		org.inasnet.user.ContactGroup contactGroup = groupsIter.next();
		
		while( true )
		{
			needRows += contactGroup.getUsersList().size() + 1;
			
			if( ! groupsIter.hasNext() )
				break;
			
			contactGroup = groupsIter.next();
		}
				
		// set data 
		
		//Object [][] rowData = new Object[ needRows ][ 3 ];
		
		int iter = 0;
		
		groupsIter = Root.getUser().getContactList().getGroupsList().iterator();
		
		contactGroup = groupsIter.next();
		
	
		
		//ButtonGroup radioButtonGroup = new ButtonGroup();
		
		
		FlowLayout fl = new FlowLayout();
		fl.setAlignment( FlowLayout.LEFT );
		
		getContentPane().setLayout( fl );
		
		while( true )
		{
			// set the group
			
			
			//MenuItem contactGroupMI = new MenuItem( contactGroup.getName(), menu );
			
			
			

			//ContactGroupBar groupBar = addPanel( Root.lang( "MAIN_Group" ) + ": " + contactGroup.getName() + " - " + contactGroup.getUsersList().size() + "/", contactGroup );// = new ContactGroupBar( contactGroup );
	
			//getContentPane().add( groupBar );
			
			//iter++;
			
			Iterator< User > baseUserIter = contactGroup.getUsersList().iterator();
			
			User users = baseUserIter.next();
			
			while( true )
			{
				//JRadioButtonMenuItem contactUserMI = new JRadioButtonMenuItem( "" + users.getId() );
				//menu.add( contactUserMI );
				
				UserConnButton button = new UserConnButton( "" + users.getId(), users );
				button.setSize( new Dimension( 2000, 20 ) );
				//button.
				button.addActionListener( this );
				
				getContentPane().add( button );
				
				//radioButtonGroup.add( contactUserMI );


				//contactGroupMI.add( contactUserMI );
				
				//ContactUserBar userBar  = addPanel( "" + users.getId(), users );
				
				
				//groupBar.add( userBar ); 
				
				iter++;
				
				if( ! baseUserIter.hasNext() )
					break;
				
				users = baseUserIter.next();			
			}
			
			//radioButtonGroup.add( contactGroupMI );
			
			//contacts.add( contactGroupMI );
						
			if( ! groupsIter.hasNext() )
				break;
			
			contactGroup = groupsIter.next();
		}

		
		//setJMenuBar( menuBar );
		//menuBar.setBorder(new BevelBorder(BevelBorder.RAISED));
		
		/*
		 * End Main content
		 */
		
		//mSettingsFrame = new SettingsFrame();
		//mSettingsFrame.setVisible( true );
		
		

		setVisible( true );
	}
		
	private void contactUser( User user ) 
	{
		Root.log( "Want to connect with user: " + user.getId() );
		
		Root.getUser().contactUser( user );
		
		// TODO connect
		
	}
	
	public void actionPerformed( ActionEvent e )
	{
		if( e.getSource() instanceof UserConnButton )
		{
			UserConnButton b = ( UserConnButton ) e.getSource();
			
			b.setEnabled( false );
			
			if( mCurConnection != null )
				mCurConnection.setEnabled( true );
			
			mCurConnection = b;
			
			contactUser( mCurConnection.getUser() );
		}
	}

	Object mSelectedObject;	
	
	final Color mSelectedItemBGColor = new Color( 210, 210, 210 );
	final Color mCustomItemBGColor = new Color( 230, 230, 230 );
	
	//final Color mSelectedGroupBGColor = new Color( 210, 210, 210 );
	//final Color mCustomGroupBGColor = new Color( 230, 230, 230 );
	
	/* Shell mShell; */
	
	//Tree mContactsTree;
	
	SettingsFrame mSettingsFrame;
	
	ClientUser mUser;
	
	UserConnButton mCurConnection; 
	
	final int mFrameWidth = 300;
	
	final int mLineWidth = mFrameWidth + 10;
	
	final int mFrameHeight = 500;
	
	String mTitle = "INASnet Client";
}
